<template>
  <div class="id-test-container">
    <el-card class="test-card">
      <template #header>
        <div class="card-header">
          <span>用户ID格式测试</span>
          <el-button type="primary" @click="refreshUserInfo">刷新用户信息</el-button>
        </div>
      </template>

      <el-alert
        v-if="error"
        :title="error"
        type="error"
        show-icon
        :closable="false"
        class="mb-4"
      />

      <el-alert
        v-if="success"
        :title="success"
        type="success"
        show-icon
        :closable="false"
        class="mb-4"
      />

      <!-- 当前用户信息 -->
      <el-descriptions title="当前用户信息" :column="2" border class="mb-4">
        <el-descriptions-item label="用户ID">
          <el-tag :type="idType === 'string' ? 'success' : 'warning'">
            {{ userInfo.id }} ({{ idType }})
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="用户名">{{ userInfo.username }}</el-descriptions-item>
        <el-descriptions-item label="真实姓名">{{ userInfo.realName }}</el-descriptions-item>
        <el-descriptions-item label="角色">{{ userInfo.role }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ userInfo.email }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ userInfo.phone }}</el-descriptions-item>
      </el-descriptions>

      <!-- ID格式测试 -->
      <el-divider content-position="left">ID格式测试</el-divider>
      
      <el-row :gutter="20" class="mb-4">
        <el-col :span="12">
          <el-card>
            <template #header>原始ID值</template>
            <div class="id-display">
              <p><strong>值:</strong> {{ userInfo.id }}</p>
              <p><strong>类型:</strong> {{ typeof userInfo.id }}</p>
              <p><strong>长度:</strong> {{ String(userInfo.id).length }}</p>
              <p><strong>是否为数字:</strong> {{ !isNaN(userInfo.id) }}</p>
              <p><strong>是否安全整数:</strong> {{ Number.isSafeInteger(Number(userInfo.id)) }}</p>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="12">
          <el-card>
            <template #header>转换后的ID值</template>
            <div class="id-display">
              <p><strong>String():</strong> {{ String(userInfo.id) }}</p>
              <p><strong>Number():</strong> {{ Number(userInfo.id) }}</p>
              <p><strong>BigInt():</strong> {{ BigInt(userInfo.id) }}</p>
              <p><strong>JSON.stringify:</strong> {{ JSON.stringify(userInfo.id) }}</p>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 测试操作 -->
      <el-divider content-position="left">测试操作</el-divider>
      
      <el-row :gutter="20" class="mb-4">
        <el-col :span="8">
          <el-button type="primary" @click="testUpdateWithStringId" :loading="loading">
            测试字符串ID更新
          </el-button>
        </el-col>
        <el-col :span="8">
          <el-button type="warning" @click="testUpdateWithNumberId" :loading="loading">
            测试数字ID更新
          </el-button>
        </el-col>
        <el-col :span="8">
          <el-button type="info" @click="testUpdateWithBigIntId" :loading="loading">
            测试BigInt ID更新
          </el-button>
        </el-col>
      </el-row>

      <!-- 测试结果 -->
      <el-divider content-position="left">测试结果</el-divider>
      
      <el-table :data="testResults" style="width: 100%">
        <el-table-column prop="test" label="测试类型" width="150" />
        <el-table-column prop="inputId" label="输入ID" width="200" />
        <el-table-column prop="inputType" label="输入类型" width="100" />
        <el-table-column prop="result" label="结果" />
        <el-table-column prop="timestamp" label="时间" width="180" />
      </el-table>

      <!-- 调试信息 -->
      <el-divider content-position="left">调试信息</el-divider>
      
      <el-card>
        <template #header>控制台日志</template>
        <pre class="debug-log">{{ debugLog }}</pre>
      </el-card>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { updateUserInfo } from '@/api/user'

const store = useStore()
const loading = ref(false)
const error = ref('')
const success = ref('')
const testResults = ref([])
const debugLog = ref('')

const userInfo = computed(() => store.state.user.userInfo || {})
const idType = computed(() => typeof userInfo.value.id)

// 添加调试日志
const addDebugLog = (message) => {
  const timestamp = new Date().toLocaleTimeString()
  debugLog.value += `[${timestamp}] ${message}\n`
  console.log(message)
}

// 刷新用户信息
const refreshUserInfo = async () => {
  try {
    loading.value = true
    error.value = ''
    success.value = ''
    
    addDebugLog('开始刷新用户信息...')
    await store.dispatch('user/refreshUserInfo')
    addDebugLog('用户信息刷新成功')
    
    success.value = '用户信息刷新成功'
  } catch (err) {
    addDebugLog(`刷新用户信息失败: ${err.message}`)
    error.value = `刷新失败: ${err.message}`
  } finally {
    loading.value = false
  }
}

// 测试更新 - 字符串ID
const testUpdateWithStringId = async () => {
  await testUpdate('字符串ID', String(userInfo.value.id))
}

// 测试更新 - 数字ID
const testUpdateWithNumberId = async () => {
  await testUpdate('数字ID', Number(userInfo.value.id))
}

// 测试更新 - BigInt ID（如果支持）
const testUpdateWithBigIntId = async () => {
  try {
    // eslint-disable-next-line no-undef
    if (typeof BigInt !== 'undefined') {
      // eslint-disable-next-line no-undef
      await testUpdate('BigInt ID', BigInt(userInfo.value.id))
    } else {
      error.value = '当前环境不支持BigInt'
      addDebugLog('BigInt not supported in this environment')
    }
  } catch (err) {
    error.value = `BigInt测试失败: ${err.message}`
    addDebugLog(`BigInt test failed: ${err.message}`)
  }
}

// 通用测试更新函数
const testUpdate = async (testType, inputId) => {
  try {
    loading.value = true
    error.value = ''
    success.value = ''
    
    addDebugLog(`开始测试: ${testType}`)
    addDebugLog(`输入ID: ${inputId} (类型: ${typeof inputId})`)
    
    const testData = {
      id: inputId,
      realName: userInfo.value.realName,
      email: userInfo.value.email,
      phone: userInfo.value.phone
    }
    
    addDebugLog(`发送数据: ${JSON.stringify(testData)}`)
    
    const response = await updateUserInfo(testData)
    
    addDebugLog(`更新成功: ${JSON.stringify(response)}`)
    
    testResults.value.unshift({
      test: testType,
      inputId: String(inputId),
      inputType: typeof inputId,
      result: '成功',
      timestamp: new Date().toLocaleString()
    })
    
    success.value = `${testType}测试成功`
    
  } catch (err) {
    addDebugLog(`${testType}测试失败: ${err.message}`)
    
    testResults.value.unshift({
      test: testType,
      inputId: String(inputId),
      inputType: typeof inputId,
      result: `失败: ${err.message}`,
      timestamp: new Date().toLocaleString()
    })
    
    error.value = `${testType}测试失败: ${err.message}`
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  addDebugLog('ID测试页面加载完成')
  addDebugLog(`当前用户ID: ${userInfo.value.id} (类型: ${typeof userInfo.value.id})`)
})
</script>

<style scoped>
.id-test-container {
  padding: 20px;
}

.test-card {
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.id-display {
  font-family: monospace;
  font-size: 14px;
}

.id-display p {
  margin: 5px 0;
}

.debug-log {
  background-color: #f5f5f5;
  padding: 10px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 12px;
  max-height: 300px;
  overflow-y: auto;
  white-space: pre-wrap;
}

.mb-4 {
  margin-bottom: 16px;
}
</style> 